import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 涛
 * Date: 2023-09-28
 * Time: 20:20
 */
public class MySingleList implements IList{
    //定义一个节点
    public class ListNode {
        public int val;
        public ListNode next;//下一个节点

        public ListNode(int val) {
            this.val = val;
        }
    }
    //头结点
    public ListNode head;
    //创建5个节点
    public void createList() {
        ListNode node1 = new ListNode(12);
        ListNode node2 = new ListNode(23);
        ListNode node3 = new ListNode(34);
        ListNode node4 = new ListNode(45);
        ListNode node5 = new ListNode(56);

        node1.next = node2;
        node2.next = node3;
        node3.next = node4;
        node4.next = node5;
        //定义头结点
        this.head = node1;
    }

    @Override
    public void addFirst(int data) {
        //头插
        ListNode cur = new ListNode(data);
        //如果一个节点都没有
        if(this.head == null) {
            this.head = cur;
            return;
        }
        //有其他节点
        cur.next = this.head;
        this.head = cur;
    }

    @Override
    public void addLast(int data) {
        //尾插
        ListNode node = this.head;
        ListNode cur = new ListNode(data);
        //一个节点都没有
        if(this.head == null) {
            this.head = cur;
            return;
        }

        //先找到尾巴(最后一个节点)
        int count = 0;
        while(count < size() - 1) {
            node = node.next;
            count++;
        }
        node.next = cur;
    }

    @Override
    public void addIndex(int index, int data) {
        ListNode cur = new ListNode(data);
        ListNode node = head;
        //判断下标index是否合法
        try {
            if(index < 0 || index > size()) {
                //抛一个异常
                throw new ListException("下标不合法");
            }
        } catch (ListException e) {
            e.printStackTrace();
        }
        //如果一个节点都没有
        if(this.head == null) {
            this.head = node;
            return;
        }
        //有其他节点
        //如果是首节点
        if(index == 0) {
            //头插
            addFirst(data);
            return;
        }
        //如果是尾结点
        if (index == size()) {
            addLast(data);
            return;
        }
        //中间节点
        //先找到index下标节点的前驱
        node = searchPrev(index);
        //插入
        ListNode nodeNext = node.next;
        node.next = cur;
        cur.next = nodeNext;
    }
    private ListNode searchPrev(int index) {
        ListNode cur = this.head;
        int count = 0;
        while(count < index - 1) {
            cur = cur.next;
        }
        return cur;
    }

    @Override
    public boolean contains(int key) {
        //遍历一遍数组，有返回true，没有返回false
        ListNode cur = this.head;
        ListNode node = new ListNode(key);
        while(cur != null) {
            if(cur.val == node.val) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    @Override
    public void remove(int key) {
        //一个节点都没有
        if(this.head == null) {
            //抛一个异常
            return;
        }
        //首节点
        if(this.head.val == key) {
            this.head = head.next;
            return;
        }

        //看是否包含key的值，有，找到key值的前驱，没有返回退出
        ListNode prev = findPrev(key);
        if(prev == null) {
            System.out.println("没有你要找的数字");
            return;
        }
        //删除
        ListNode del = prev.next;
        prev.next = del.next;

    }
    private ListNode findPrev(int key) {
        ListNode node = this.head;
        //遍历一遍，有key值返回这个节点的前驱，没有，返回null
        while(node.next != null) {
            //找前驱
            if(node.next.val == key) {
                return node;
            }
            node = node.next;
        }
        return null;
    }

    @Override
    public void removeAllKey(int key) {
        //一个节点都没有
        if(this.head == null) {
            return;
        }
        ListNode prev = this.head;
        ListNode cur = prev.next;
        while (cur != null) {
            //判断当前节点的值是否为key
            if(cur.val == key) {
                prev.next = cur.next;
                cur = cur.next;
            } else {
                prev = cur;
                cur = cur.next;
            }
        }
        //判断头结点
        if(this.head.val == key) {
            this.head = head.next;
        }
    }

    @Override
    public int size() {
        ListNode cur = this.head;
        int count = 0;
        while(cur != null) {
            cur = cur.next;
            count++;
        }
        return count;
    }

    @Override
    public void clear() {
        //如果没有节点
        if(this.head == null) {
            return;
        }
        ListNode cur = this.head;
        while(cur != null) {
            ListNode curNext = cur.next;
            cur.next = null;
            //cur.val = null;
            cur = curNext;
        }
        this.head = null;
    }

    @Override
    public void display() {
        ListNode cur = this.head;

        while(cur != null) {
            System.out.println(cur.val+" ");
            cur = cur.next;
        }
        System.out.println();
    }
}
